package com.zretc.support.controller;

import com.zretc.support.entity.Topicmult;
import com.zretc.support.service.CourseService;
import com.zretc.support.service.TopicmultService;
import com.zretc.support.tool.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class TopicmultController {

    @Autowired
    TopicmultService topicmultService;
    @Autowired
    CourseService courseService;

    /**
     * 分页
     * @param keyword
     * @param level
     * @param type
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "topicmult",method = RequestMethod.GET)
    public PageData getTopicmultByPage(@RequestParam(defaultValue = "",required = false) String keyword,
                                       @RequestParam(defaultValue = "",required = false) String level,
                                       @RequestParam(defaultValue = "",required = false) String type,
                                       @RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int size){

        PageData pageData = new PageData();
        Page pages = topicmultService.getTopicmultByPage(keyword, level, type, page, size);
        List<Topicmult> list = pages.getContent();
        pageData.setRecords(list); // 数据
        pageData.setCurrent(page); // 当前页
        pageData.setSize(size); // 每页数量
        pageData.setTotal((int)pages.getTotalElements()); // 总记录数
        return pageData;
    }

    /**
     * 分页
     * @param keyword
     * @param level
     * @param type
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "topicmultbycourse",method = RequestMethod.GET)
    public PageData getTopicmultCourseByPage(@RequestParam(defaultValue = "",required = false) String keyword,
                                       @RequestParam(defaultValue = "",required = false) String level,
                                       @RequestParam(defaultValue = "",required = false) String type,
                                       @RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int size){
        PageData pageData = new PageData();
        Page pages = null;
        if (keyword.equals("")){
            pages = topicmultService.getTopicmultByPage(keyword, level, type, page, size);
        }else {
            List<Long> courseId = courseService.getCourseByCourseName(keyword);
            pages = topicmultService.getTopicmultByCourseIdWithPage(courseId, level, type, page, size);
        }
        List<Topicmult> list = pages.getContent();
        pageData.setRecords(list); // 数据
        pageData.setCurrent(page); // 当前页
        pageData.setSize(size); // 每页数量
        pageData.setTotal((int)pages.getTotalElements()); // 总记录数
        return pageData;
    }

    /**
     * 根据试题编号查询试题信息
     * @param topicId
     * @return
     */
    @RequestMapping(value = "getMultById/{topicId}",method = RequestMethod.GET)
    public Topicmult getTopicsingleByTopicId(@PathVariable("topicId") String topicId){
        return topicmultService.getTopicmultById(topicId);
    }

    /**
     * 根据试题编号删除试题信息
     * @param topicId
     * @return
     */
    @RequestMapping(value = "delMultById/{topicId}",method = RequestMethod.DELETE)
    public int delTopicsingleByTopicId(@PathVariable("topicId") String topicId){
        return topicmultService.delTopicmult(topicId);
    }

    /**
     * 修改试题信息
     * @param topicmult
     * @return
     */
    @RequestMapping(value = "updMultById",method = RequestMethod.PUT)
    public int updTopicsingleByTopicId(@RequestBody Topicmult topicmult){
        return topicmultService.updTopicmult(topicmult);
    }

    /**
     * 增加试题信息
     * @param topicmult
     * @return
     */
    @RequestMapping(value = "addTopicmult",method = RequestMethod.PUT)
    public int addTopicsingle(@RequestBody Topicmult topicmult){
        if (topicmultService.addTopicmult(topicmult)!=null){
            return 1;
        }
        return 0;
    }




}
